package com.second.controller.personal;

import com.second.consts.PersonalConst;
import com.second.mapper.FCollectionMapper;
import com.second.mapper.FCommentMapper;
import com.second.mapper.FCommentZanMapper;
import com.second.mapper.FHistoryMapper;
import com.second.model.dto.personal.FShopCollection;
import com.second.model.pojo.FHistory;
import com.second.model.pojo.FUser;
import com.second.moudle.Result;
import com.second.service.IFCollectionService;
import com.second.service.IFCommentService;
import com.second.service.IFHistoryService;
import com.second.service.IFUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("personal")
public class personalController {
    @Autowired
    private IFUserService userService;

    @Autowired
    private IFCollectionService collectionService;

    @Autowired
    private FCommentZanMapper commentZanMapper;

    @Autowired
    private FCollectionMapper collectionMapper;

    @Autowired
    private FCommentMapper commentMapper;

    @Autowired
    private IFHistoryService historyService;

    @Autowired
    HttpServletRequest request;

    @GetMapping()
    public String personalCenter(){
        return "jsp/personal/personal-center";
    }

    /**
     * 个人信息修改
     * @param user
     * @return
     */
    @PostMapping("personalUpdate")
    @ResponseBody
    public Result personalInfo(@RequestBody FUser user,HttpServletRequest httpServletRequest){
        FUser user1 = userService.changeInfo(user);
        HttpSession session = httpServletRequest.getSession();
        session.setAttribute("user",user1);
        return new Result("200","修改成功",user1);
    }

    /**
     * @param resp
     * @throws IOException
     */
    @GetMapping("personalFlash")
    public void personalFlash(HttpServletResponse resp) throws IOException {
        resp.sendRedirect("http://localhost:8081/fr/personal?page=3");
    }

    /**
     * 用户收藏列表分页
     * service中写了默认四个一页
     * @return
     */
    @PostMapping("collections/{pageNum}")
    @ResponseBody
    public Result Collections(@PathVariable(name = "pageNum") Integer pageNum){
        if ( null == pageNum) {
            pageNum = 1;
        }
        HttpSession session = request.getSession();
        FUser user = (FUser) session.getAttribute("user");
        List<FShopCollection> list = collectionService.findCollectionsPage(user.getId(),pageNum-1);
        return new Result("200","查询成功",list);
    }

    /**取消收藏
     * @param shopId
     * @param userId
     * @param resp
     * @throws IOException
     */
    @PostMapping("cancel/{shopId}/{userId}")
    @ResponseBody
    public void cancelCollection(@PathVariable(name = "shopId") Integer shopId,@PathVariable(name = "userId") Integer userId,
                                   HttpServletResponse resp) throws IOException {
        collectionService.cancelCollection(shopId,userId);
        resp.sendRedirect("http://localhost:8081/fr/personal?page=2");
    }

    /**
     * 我的消息
     * @param userId
     * @return
     */
    @PostMapping("myMessage/{userId}")
    @ResponseBody
    public Result myMessage(@PathVariable(name = "userId") Integer userId){
        return new Result("200","消息获取成功",userService.getThreeMessage(userId));
    }

    /**
     * 我的动态
     * @param userId
     * @return
     */
    @PostMapping("myRecent/{userId}")
    @ResponseBody
    public Result myRecent(@PathVariable(name = "userId") Integer userId){
        return new Result("200","消息获取成功",userService.findThreeComments(userId));
    }

    /**
     * 我的订单
     * @param userId
     * @return
     */
    @PostMapping("myOrder/{userId}")
    @ResponseBody
    public Result myOrder(@PathVariable(name = "userId") Integer userId){
        return new Result("200","消息获取成功",userService.getFourOrders(userId));
    }

    /**
     * 我的点评数
     * @param userId
     * @return
     */
    @PostMapping("myComments/{userId}")
    @ResponseBody
    public Result myComments(@PathVariable(name = "userId") Integer userId){
        return new Result("200","查询成功",commentMapper.countByUserId(userId));
    }

    /**
     * 收到点赞数
     * @param userId
     * @return
     */
    @PostMapping("myZan/{userId}")
    @ResponseBody
    public Result myZan(@PathVariable(name = "userId") Integer userId){
        return new Result("200","查询成功",commentZanMapper.countByUserIdAndStatus(userId, PersonalConst.Zan));
    }

    /**
     * 我的收藏数
     * @param userId
     * @return
     */
    @PostMapping("myCollections/{userId}")
    @ResponseBody
    public Result myCollections(@PathVariable(name = "userId") Integer userId){
        return new Result("200","查询成功",collectionMapper.countByUserIdAndAndStatus(userId,PersonalConst.Collected_Status));
    }

    @PostMapping("history/{userId}/{shopId}")
    @ResponseBody
    public Result history(@PathVariable(name = "userId") Integer userId,@PathVariable(name = "shopId") Integer shopId){
        return new Result("200","查询成功",historyService.addHistory(userId,shopId));
    }

}
